package com.jdew.thkj.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.jdew.thkj.R;
import com.jdew.thkj.manager.ActivityManagerUtils;
import com.jdew.thkj.view.X5WebView;

import butterknife.Bind;
import butterknife.ButterKnife;


public class TxWebViewActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    private X5WebView mX5WebView;
    private String url;
    private String head;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tx_webview);
        ButterKnife.bind(this);
        StatusBarUtil.setTransparentForImageView(this, null);
        ActivityManagerUtils.pushActivity(this);
        mX5WebView = findViewById(R.id.x5_webview);
        head = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        back.setOnClickListener(this);
        title.setText(head);
        initHardwareAccelerate();
        initView();
    }

    /**
     * 启用硬件加速
     */
    private void initHardwareAccelerate() {
        try {
            if (Integer.parseInt(Build.VERSION.SDK) >= 11) {
                getWindow()
                        .setFlags(
                                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }
    }

    private void initView() {
        mX5WebView = findViewById(R.id.x5_webview);
        mX5WebView.loadUrl(url);
    }

    /**
     * 返回键监听
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mX5WebView != null && mX5WebView.canGoBack()) {
                mX5WebView.goBack();
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        //释放资源
        if (mX5WebView != null)
            mX5WebView.destroy();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:{
                finish();
            }
            break;
        }
    }
}
